package org.marsdonne.sync;

import com.fasterxml.jackson.databind.JsonNode;
import lombok.Getter;
import org.marsdonne.commons.IdGenerator;
import org.marsdonne.commons.Validatable;

import javax.validation.ConstraintViolation;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.PastOrPresent;
import java.io.Serializable;
import java.util.Date;
import java.util.Set;

@Getter
public class Event implements Serializable, Validatable {

    @NotNull
    private Action action;

    @NotBlank
    private String domain;

    @PastOrPresent
    private Date dateSynced;

//    @Size(min = 2)
    private JsonNode content;

    private Long version;

    public Event(Action action, String domain, Date dateSynced, JsonNode content) {
        this.action = action;
        this.domain = domain;
        this.dateSynced = dateSynced;
        this.content = content;

        this.version = IdGenerator.newInstance().nextId();
    }

    @Override
    public String toString() {
        return "Event{" +
                "action=" + action +
                ", domain='" + domain + '\'' +
                ", dateSynced=" + dateSynced +
                '}';
    }

    public Set<ConstraintViolation<Event>> validate() {
        return this.validate(this);
    }
}
